package com.bilin.coin.service;

import android.annotation.TargetApi;
import android.app.IntentService;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.bilin.coin.R;
import com.bilin.coin.network.RequestUtils;
import com.bilin.coin.network.download.DownLoadListener;
import com.bilin.coin.utils.FilePathUtils;
import com.bilin.coin.utils.MobilePhoneDeviceInfo;

import java.io.File;


/**
 * Created by pundix002 on 2018/5/9.
 */

public class DownloadFileService extends IntentService {
    private static final String CHANNEL_ID = "download";
    private NotificationManager mNotificationManager;
    private NotificationCompat.Builder builder;
    private Context mContext;
    private String strLink;

    public DownloadFileService() {
        super("DownloadFileService");
        mContext = this;
    }


    @Override
    public void onCreate() {
        super.onCreate();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelName = "文件下载";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(CHANNEL_ID, channelName, importance);
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }

    @Override
    public void onStart(@Nullable Intent intent, int startId) {
        super.onStart(intent, startId);
        strLink = intent.getStringExtra("url");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        builder = new NotificationCompat.Builder(this, CHANNEL_ID);
        builder.setContentTitle(getString(R.string.download_process));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        mNotificationManager.notify(90, builder.build());
        downloadFile();
    }

    public void downloadFile() {
        RequestUtils
                .getFile()
                .url(strLink)
                .downLoadFile(FilePathUtils.accessPath(mContext, "downloadFile").getPath(), "Version" + MobilePhoneDeviceInfo.getVersionName(mContext) + "wc.apk", new DownLoadListener() {
                    @Override
                    public void onStartDownload() {

                    }

                    @Override
                    public void onProgress(final int progress) {
                        builder.setProgress(100, progress, false);
                        builder.setContentText(mContext.getString(R.string.download_already) + progress + "%");
                        mNotificationManager.notify(90, builder.build());
                    }

                    @Override
                    public void onFinishDownload(File file) {
                        builder.setContentTitle(getString(R.string.download_complete));//下载完成
                        builder.setProgress(100, 100, false);
                        builder.setContentText(mContext.getString(R.string.download_already) + "100 %");
                        mNotificationManager.notify(90, builder.build());
                        FilePathUtils.openFile(mContext, file);
                    }

                    @Override
                    public void onFail(String errorInfo) {

                    }
                });
    }

}
